package com.product.config;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @author Hanhuiyong
 * @version 1.0.0
 * @ClassName MyTablePreciseShardingAlgorithm.java
 * @Description
 * @createTime 2021年09月13日 11:15:00
 */
@Component
public class MyDataBasePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> databaseNames, PreciseShardingValue<Long> shardingValue) {

        char dbIndex = shardingValue.getValue().toString().charAt(1);
        String shardDbName = "";
        for (String databaseName : databaseNames) {
            if (databaseName.endsWith(String.valueOf(dbIndex))) {
                return databaseName;
            }
        }
        return shardDbName;
    }
}
